package com.hmh.student.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.system.entity.SysMenu;
import com.hmh.student.system.entity.SysRole;
import com.hmh.student.system.entity.SysRoleMenu;
import com.hmh.student.system.mapper.SysMenuMapper;
import com.hmh.student.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmh.student.system.service.ISysRoleMenuService;
import com.hmh.student.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyf
 * @since 2025-04-09
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {




    @Override
    public Page<SysMenu> queryMenuPage(String menuName, Integer parentId, Integer pageSize, Integer current) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        if (menuName != null && !menuName.isEmpty()) {
            queryWrapper.like("menu_name", menuName);
        }
        if (parentId != null) {
            queryWrapper.eq("parent_id", parentId);
        }
        queryWrapper.orderByAsc("sort_order");
        return this.page(new Page<>(current, pageSize), queryWrapper);
    }

    @Override
    public Map<String, List<SysMenu>> getAllMenu() {
        // 查询所有菜单，按sort_order排序
        List<SysMenu> menus = this.list(new QueryWrapper<SysMenu>().orderByAsc("sort_order"));

        // 按父菜单ID分组
        Map<String, List<SysMenu>> menuMap = menus.stream()
                .collect(Collectors.groupingBy(menu -> String.valueOf(menu.getParentId())));

        return menuMap;
    }


}
